package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.limao.DBDataClass.MobileCodeDataClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;
import com.limao.Tool.Function;

public class MobileCodeProvider {

	private static class DB_mobile_code_ProviderContainer {
		private static MobileCodeProvider instance = new MobileCodeProvider();
	}
	public static MobileCodeProvider getInstance() {
		return DB_mobile_code_ProviderContainer.instance;
	}

	private static final String LOAD_CODE = "SELECT * FROM cd_mobile_code WHERE mobile=?";
	private static final String UPDATE_CODE = "UPDATE cd_mobile_code SET sendnum = sendnum +1,"
												+"code = ?, "
												+"loadcodetime=? WHERE mobile=?";
	private static final String INSERT_CODE = "INSERT INTO cd_mobile_code(mobile,code,validity_date,"
													+"loadcodetime,sendnum) VALUES (?,?,?,?,?)";
	private static final String DETELE_CODE_TimeOut = "DELETE FROM cd_mobile_code WHERE validity_date<?";
	private static final String DETELE_CODE_Mobile = "DELETE FROM cd_mobile_code WHERE mobile=?";

	// 初始化
	private MobileCodeProvider() { }

	private MobileCodeDataClass encapMobileCode(ResultSet rs) throws Exception{
		MobileCodeDataClass mobileCode = new MobileCodeDataClass();
		mobileCode = new MobileCodeDataClass();
		mobileCode.code_id = rs.getInt("id") + "";							// 自增id
		mobileCode.code_mobile = rs.getString("mobile");					// 手机号
		mobileCode.code_code = rs.getString("code");						// 验证码
		mobileCode.code_validity_date = rs.getLong("validity_date") + "";	// 截止时间值
		mobileCode.loadcodetime = rs.getLong("loadcodetime") + "";			// 上次获取验证码的时间
		mobileCode.sendnum = rs.getInt("sendnum") + "";						// 半小时内发送短信次数
		return mobileCode;
	}
	
	// 加载某个用户的验证码
	public MobileCodeDataClass Load_Mobile_Code(String mobile) {
		MobileCodeDataClass mobileCode = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(LOAD_CODE);
			pstmt.setString(1, mobile);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				mobileCode = encapMobileCode(rs);
			}
		} catch (Exception e) {
			mobileCode = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return mobileCode;
	}

	public MobileCodeDataClass createCode(String mobile, String code) {
		MobileCodeDataClass mobileCode = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(INSERT_CODE);
			pstmt.setString(1, mobile);
			pstmt.setString(2, code);
			Long current = CacheClass.getTime();
			Long validtime = current + Function.VALIDITY * 60 * 1000;
			pstmt.setLong(3, validtime);
			pstmt.setLong(4, current);
			pstmt.setInt(5, 1);

			pstmt.execute();

			mobileCode = new MobileCodeDataClass();
			mobileCode.code_mobile = mobile;
			mobileCode.code_code = code;
			mobileCode.code_validity_date = validtime + "";
			mobileCode.loadcodetime = current + "";
			mobileCode.sendnum = "1";
		} catch (Exception e) {
			mobileCode = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		
		return mobileCode;
	}

	public MobileCodeDataClass updateCodeTime(String mobile, String code, long loadcodetime) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		MobileCodeDataClass mobileCode = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(UPDATE_CODE);
			pstmt.setString(1, code);
			pstmt.setLong(2, loadcodetime);
			pstmt.setString(3, mobile);
			pstmt.executeUpdate();
			
			DbConnectionManager.fastcloseStmt(rs,pstmt);
			pstmt = con.prepareStatement(LOAD_CODE);
			pstmt.setString(1, mobile);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				mobileCode = encapMobileCode(rs);
			}
		} catch (Exception e) {
			mobileCode = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return mobileCode;
	}

	public boolean deleteMobileCode(String mobile) {
		boolean tmp = false;
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(DETELE_CODE_Mobile);
			pstmt.setString(1, mobile);
			pstmt.execute();
			tmp = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(pstmt, con);
		}
		return tmp;
	}

	public void deleteExpiredCode() {
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(DETELE_CODE_TimeOut);
			pstmt.setLong(1, CacheClass.getTime());

			pstmt.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(pstmt, con);
		}
	}

}
